var port = require('../../modules/port.js');

function getBeforeDate(n) {
    var n = n;
    var d = new Date();
    var year = d.getFullYear();
    var mon = d.getMonth() + 1;
    var day = d.getDate();
    var week = d.getDay();
    if (day <= n) {
        if (mon > 1) {
            mon = mon - 1;
        }
        else {
            year = year - 1;
            mon = 12;
        }
    }
    d.setDate(d.getDate() - n);
    year = d.getFullYear();
    mon = d.getMonth() + 1;
    day = d.getDate();
    week = d.getDay();
    switch (week) {
        case 0:
            week = '周天'
            break;
        case 1:
            week = '周一'
            break;
        case 2:
            week = '周二'
            break;
        case 3:
            week = '周三'
            break;
        case 4:
            week = '周四'
            break;
        case 5:
            week = '周五'
            break;
        case 6:
            week = '周六'
            break;
    }
    if (n == 0) {
        week = '今天'
    } else if (n == 1) {
        week = '昨天'
    }
    var date = {
        time: year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day),
        week: week
    }
    return date;
}

var reach = true;
var pageNum = 1;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        dateList: [],
        dateHover: 0,
        updateData: []
    },

    dataRequest: function (date, pn) {
        wx.showNavigationBarLoading();
        var that = this;
        if (typeof date == "object") {
            date = date.time
        }
        wx.request({
            url: port.dataPort.updateData,
            data: { date: date, pageNum: pn },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
            // header: {}, // 设置请求的 header    
            success: function (data) {
                var listData = data.data.data;
                var vi;
                for (vi in listData) {
                    listData[vi].like = that.numberSwitch(listData[vi].like);
                }
                that.setData({
                    updateData: data.data.data
                })
                wx.hideNavigationBarLoading();
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var dateList = [];
        for (var i = 0; i < 7; i++) {
            dateList[i] = getBeforeDate(i);
        }
        this.setData({
            dateList: dateList
        })

        var date = getBeforeDate(0);
        this.dataRequest(date, 1);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        var date = this.data.dateList[this.data.dateHover].time;
        pageNum++;
        if (reach) {
            reach = false;
            wx.showNavigationBarLoading();
            wx.request({
                url: port.dataPort.updateData,
                data: { date: date, pageNum: pageNum },
                method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT    
                // header: {}, // 设置请求的 header    
                success: function (data) {
                    var idx = '';
                    var listData = data.data.data;
                    var vi;
                    for (vi in listData) {
                        listData[vi].like = that.numberSwitch(listData[vi].like);
                    }
                    for (idx in listData) {
                        that.data.updateData.push(data.data.data[idx]);
                    }
                    that.setData({
                        updateData: that.data.updateData
                    })
                    wx.hideNavigationBarLoading();
                    reach = true;
                }
            })
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        setTimeout(function () {
            wx.stopPullDownRefresh();
        }, 2000)
    },
    articleTap: function (e) {
        wx.navigateTo({
            url: '../content/content?id=' + e.currentTarget.dataset.id
        })
    },
    articleRequest: function (e) {
        if (e.currentTarget.dataset.id != this.data.dateHover) {
            pageNum = 1;
            var dateNum = e.currentTarget.dataset.date;
            this.dataRequest(dateNum, 1);
            this.setData({
                dateHover: e.currentTarget.dataset.id
            })
        }
    },
    numberSwitch: function (str) {
        if (str >= 10000) {
            str = str / 10000;
            return str.toFixed(1) + '万';
        } else {
            return str;
        }
    }
})